package org.com.ashen.service.service.smschargemanage;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.digest.DigestUtils;
import org.com.ashen.client.service.ISMSChargeService;
import org.com.ashen.common.common.HttpResult;
import org.com.ashen.common.common.smscharge.SMSChargeDTO;
import org.com.ashen.common.common.smscharge.SMSChargeReqDTO;
import org.com.ashen.dal.dataobject.SMSBalance;
import org.com.ashen.dal.dataobject.SMSCharge;
import org.com.ashen.dal.dataobject.Users;
import org.com.ashen.service.dao.SMSBalanceDao;
import org.com.ashen.service.dao.SMSChargeDao;
import org.com.ashen.service.dao.UsersDao;
import org.com.ashen.service.util.CodeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service("sMSChargeService")
public class SMSChargeServiceImpl implements ISMSChargeService {

    @Resource(name = "sMSChargeDao")
    private SMSChargeDao sMSChargeDao;

    @Resource(name = "sMSBalanceDao")
    private SMSBalanceDao sMSBalanceDao;

    @Resource(name = "usersDao")
    private UsersDao usersDao;


    @Transactional
    public HttpResult<SMSChargeDTO> insert(SMSChargeReqDTO record) {
        SMSChargeDTO smsChargeDTO = new SMSChargeDTO();
        if(record == null){
            return HttpResult.failedResult(smsChargeDTO,"传入内容不能为空");
        }
        String userId = record.getUserId();
        if(userId.equals("")){
            return HttpResult.failedResult(smsChargeDTO,"用户不能为空");
        }
        Users users = usersDao.getUserByLoginId(userId);
        if(users==null){
            return HttpResult.failedResult(smsChargeDTO,"用户不存在");
        }
        String APPSecret = users.getAppsecret();
        String privateAPPSecret = record.getPrivateAPPSecret();
        String statusTime = record.getStatusTime();
        String tempAppSecret = CodeUtil.toStr(DigestUtils.md5(userId + APPSecret + statusTime));
        if(!tempAppSecret.equals(privateAPPSecret)){
            return HttpResult.failedResult(smsChargeDTO,"用户验证不通过");
        }

        SMSCharge smsCharge = new SMSCharge();
        smsCharge.setUserid(record.getUserId());
        BigDecimal realCharge = new BigDecimal(record.getRealCharge());
        BigDecimal smsChargeval = new BigDecimal(record.getSMSCharge());
        smsCharge.setRealcharge(realCharge);
        smsCharge.setSmscharge(smsChargeval);
        smsCharge.setStatus("1");
        Date statusTimeDate = null;
        try {
            statusTimeDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(record.getStatusTime());
        }catch (Exception e){}

        smsCharge.setStatustime(statusTimeDate);
        smsCharge.setMemo(record.getMemo());
        smsCharge.setId(sMSChargeDao.selectMaxId().getId() + 1);
        sMSChargeDao.insert(smsCharge);

        SMSBalance smsBalance = sMSBalanceDao.selectByPrimaryKey(userId);
        if(smsBalance==null){
            SMSBalance smsBalanceInsert = new SMSBalance();
            smsBalanceInsert.setUserid(userId);
            smsBalanceInsert.setUpdatetime(statusTimeDate);
            smsBalanceInsert.setChargebalance(smsChargeval);
            sMSBalanceDao.insert(smsBalanceInsert);
        }else {
            SMSBalance smsBalanceInsert = new SMSBalance();
            smsBalanceInsert.setUserid(userId);
            smsBalanceInsert.setUpdatetime(statusTimeDate);
            BigDecimal balance = smsBalance.getChargebalance();
            smsBalanceInsert.setChargebalance(smsChargeval.add(balance));
            sMSBalanceDao.updateByPrimaryKeySelective(smsBalanceInsert);
        }
        smsChargeDTO.setSmscharge(smsChargeval);
        smsChargeDTO.setStatus("SUCCESS");
        return HttpResult.successResult(smsChargeDTO);
    }
}
